<template>
  <div class="init-table">
    <div id="init-btn-box" class="f-c-c">
      <el-button type="success" @click="initAllTables"
        >一键初始化所有表</el-button
      >
    </div>
    <div class="f-fs-c item" v-for="(table, ind) in tables" :key="ind">
      <div class="one">
        <set-form :table="table"></set-form>
      </div>
      <div class="f-c-c-c-w-sa two">
        <set-form
          :table="foreign"
          v-for="(foreign, i) in table.foreigns"
          :key="i"
        ></set-form>
      </div>
    </div>
  </div>
</template>
<script>
import SetForm from "../part/SetForm.vue";
import { getTables, createTable } from "@/develop/assets/js/api.js";
export default {
  name: "init-table",
  components: {
    SetForm,
  },
  data() {
    return {
      tables: [],
      admin: {
        total: 10,
      },
      // count: 0,
    };
  },
  methods: {
    async initData() {
      this.tables = await getTables();
    },
    async initAllTables() {
      for (let i = 0; i < this.tables.length; i++) {
        let { name, total } = this.tables[i];
        let { code, msg } = await createTable({ name, total });
        let type = code == 1 ? "success" : "error";
        this.$message[type](msg);
        if (code != 1) {
          break;
        }
      }
    },
  },
  created() {
    this.initData();
  },
};
</script>
<style lang="scss" scoped>
#init-btn-box {
  padding-bottom: 15px;
  border-bottom: 1px solid $border-color-extra-light;
}
.item {
  padding: 20px 0;
  border-bottom: 1px solid $border-color-extra-light;
}
.one {
  width: 450px;
}
.two {
}
</style>